<?php

class EventController extends Controller
{
    //View details an event with calling AJAX
    public function renderItem($id) {
        $this->actionViews($id);
    }

    public function actionViews($id)
    {
        $this->layout=false;



        //Get event language
        $event = Translate::model()->with('event')->findByPk($id);
        //Get gallery
        $gallery = Gallery::model()->with('items')->findByPk($event->event->gallery_id);
        //Get cover image for facebook
        $cover_image_face = Item::model()->findByAttributes(array('gallery_id' => $gallery->id, 'cover_image' => 1));
        // Get Main Event
        $main_event = Event::model()->with('tickets')->findByPk($event->event->id);
        $main_event->num_view = ((int)$main_event->num_view + 1);
        $main_event->save();
        //Format video link
        $main_event['video_link'] = MyFunctionCustom::getLinkYoutube($main_event['video_link']);
        //facebook comment
       // Yii::app()->clientScript->registerMetaTag($event['main_title'], $event['main_title'], $event['main_title'], array('id'=>'meta_og_title', 'property' => 'og:title'));
       // Yii::app()->clientScript->registerMetaTag(Yii::app()->baseUrl . '/data/image/event/' . $cover_image_face['name'], '', null, array('id'=>'meta_og_image', 'property' => 'og:image'));
       // Yii::app()->clientScript->registerMetaTag('mogsn', '$event["sub_title"]', $event['sub_title'], array('id'=>'meta_og_site_name', 'property' => 'og:site_name'));
       // Yii::app()->clientScript->registerMetaTag($event['sub_title'], $event['sub_title1'], $event['sub_title1'], array('id'=>'meta_og_description', 'property' => 'og:description'));
        // var_dump($main_event['tickets']);die();
        //Render views with AJAX load without return json
        $this->render('views',array(
            'event' => $event,
            'gallery' => $gallery,
            'main_event' => $main_event
            ));
    }
    // View event details with event category
    public function actionViewcat($id)
    {

        // var_dump($id);die();
        if (isset(Yii::app()->session['_lang'])) {
            $language = Yii::app()->session['_lang'];
        }
        else{
            Yii::app()->session['_lang'] = 'vi';
            $language = Yii::app()->session['_lang'];
        }
        $languages = Language::model()->findByAttributes(array('language_code' => $language));
        $start_date = date('Y-m-d', time());
        $criteria = new CDbCriteria();
        $criteria->select = 't.*, e.num_view as num_view';
        $criteria->with= 'event';

        // $criteria->from = 'translate';
        $criteria->join='JOIN events e ON t.event_id = e.id';

        // $criteria->compare('t.language_id',$languages->id);
        // $criteria->compare('e.category_id',$id);
        $criteria->condition = 't.language_id = :lang_id AND e.category_id = :cate_id AND e.start_date >= :start_date AND e.active_status = :status';
        $criteria->params = array(':lang_id' => $languages->id,'cate_id' => $id,':start_date' => $start_date,':status' => STATUS_ACTIVE);
        $criteria->order = 'e.priority DESC ,e.start_date,e.start_time,t.main_title ASC';
        $event = Translate::model()->findAll($criteria);
        //Get all item from gallery of an event
        // var_dump($event);die();
        foreach ($event as $key => $value) {
            $image = Item::model()->findByAttributes(array('gallery_id' => $value->event->gallery_id,'cover_image' => 1));
                //Assign cover image
            if ($image != null) {
                $value['event']['cover_image'] = $image->name;
            }

        }
        $this->render('viewcat',array(
                'event' => $event,
            ));
    }
    // View details an event without AJAX
	public function actionView($id)
	{
            try{
                $this->render('view',array(
                    'model'=>$this->loadModel($id),'parentUrl'=>$this->parentUrl
                    ));
            }catch (Exception $e){
                    Yii::log("Invalid request. Please do not repeat this request again.");
                    throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
            }
	}

	public function actionCreate()
	{
            try{
                    $model=new Event;

                    if(isset($_POST['Event'])){
                            $model->attributes=$_POST['Event'];
                            $model->validate();
                            if(!$model->hasErrors()){
                                if($model->save())
                                       $this->redirect(array('view','id'=>$model->id));
                            }
                    }

                    $this->render('create',array(
                            'model'=>$model,
                    ));
            }catch (Exception $e){
                    Yii::log("Invalid request. Please do not repeat this request again.");
                    throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
            }
	}

	public function actionUpdate($id)
	{
             try{
                    $model=$this->loadModel($id);
                    if(isset($_POST['Event'])){
                            $model->attributes=$_POST['Event'];
                            $model->validate();
                            if(!$model->hasErrors()){
                                if($model->save())
                                      $this->redirect(array('view','id'=>$model->id));
                            }
                    }

                    $this->render('update',array(
                            'model'=>$model,'parentUrl'=>$this->parentUrl
                    ));
            }catch (Exception $e){
                     Yii::log("Invalid request. Please do not repeat this request again.");
                    throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
            }
	}

	public function actionDelete($id)
	{
             try{
                    $model = $this->loadModel($id);
                    if($model){
                        if($model->delete()){
                             $this->redirect(Yii::app()->createAbsoluteUrl($this->parentUrl .'/index'));
                        }
                    } else throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
            }catch (Exception $e){
                    Yii::log("Invalid request. Please do not repeat this request again.");
                    throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
            }
	}

	public function actionIndex()
	{
            try{
                    $model =new Event;
                    $data=$model->getAll();
                    $this->render('index',array(
                            'data'=>$data,'parentUrl'=>$this->parentUrl
                    ));
            }catch (Exception $e){
                     Yii::log("Invalid request. Please do not repeat this request again.");
                    throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
            }

	}

	public function loadModel($id)
	{
             try{
 		$model=Event::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
            }catch (Exception $e){
                    Yii::log("Invalid request. Please do not repeat this request again.");
                    throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
            }
	}

}
